using System;
using DbUp.Specification.Contexts;
using System.Collections.Generic;
using DbUp.ScriptProviders;
using NSubstitute;
using System.Linq;
using NUnit.Framework;

namespace DbUp.Specification
{
    public class GivenAnUptoDateDatabase : EmptyDatabase
    {
        public override void BeforeEach()
        {
            base.BeforeEach();
            AllScripts = new List<SqlScript>()
            {
                new SqlScript("0001.sql", "".ToSampleScript()),
                new SqlScript("0004.sql", "".ToSampleScript()),
                new SqlScript("0002.sql", "".ToSampleScript())
            };

            ScriptProvider.GetScripts().Returns(AllScripts);
            VersionTracker.GetExecutedScripts().Returns(new List<SqlScriptAlreadyApplied>
		                                                                         {
		                                                                             new SqlScriptAlreadyApplied(
		                                                                                 AllScripts.First().VersionKey,
		                                                                                 AllScripts.First().Name,
                                                                                         "",
		                                                                                 DateTime.Now, ""),
		                                                                             new SqlScriptAlreadyApplied(
		                                                                                 AllScripts.Skip(1).First().VersionKey,
		                                                                                 AllScripts.Skip(1).First().Name,
                                                                                         "",
		                                                                                 DateTime.Now, ""),
		                                                                             new SqlScriptAlreadyApplied(
		                                                                                 AllScripts.Skip(2).First().VersionKey,
		                                                                                 AllScripts.Skip(2).First().Name,
                                                                                         "",
		                                                                                 DateTime.Now, ""),
		                                                                         });
        }
    }

    public static class Extensions
    {
        public static string ToSampleScript(this string contents)
        {
            return ToSampleScript(contents, Guid.NewGuid());
        }
        public static string ToSampleScript(this string contents, Guid version)
        {
            return string.Format(@"-- Version: {0}{1}{2}", version, Environment.NewLine, contents);
        }
    }
}
